package persistencia;

import java.util.List;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.query.Predicate;

import espectaculos.Festival;



/**
 * TODO: description
 */
public class DBManager {
    private static DBManager inst;

    private static final String FILE_NAME = "../baseDeDatos.db4o";

    private ObjectContainer container;

    // *******************//
    // ** Constructores **//
    // *******************//
    private DBManager() {
        this.initialize();
    }

    public void initialize() {
        this.container = Db4o.openFile(FILE_NAME);
    }

    // *************************//
    // ** Getters and Setters **//
    // *************************//
    public ObjectContainer getContainer() {
        return this.container;
    }

    public static DBManager getInstance() {
        if (inst == null) {
            inst = new DBManager();
        }
        return inst;
    }

    // *************//
    // ** Metodos **//
    // *************//
    public void runOperation(final Runnable operation) {
        operation.run();
    }

    public void set(final Object obj) {
        this.container.store(obj);
    }

    /**
     * recibe una clase por parametro.
     */
    public List<? extends Object> getAll(final Class<? extends Object> clazz) {
        return this.container.query(clazz);
    }

    public List<? extends Object> query(final Predicate<? extends Object> condicion) {
        return this.container.query(condicion);
    }

    public Object get(final Object obj) {
        return this.container.queryByExample(obj).get(0);
    }

    public void close() {
        this.container.close();
    }

	public void delete(Object obj) {
		this.container.delete(obj);
	}

}
